package com.sniff.atp.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
@Slf4j
public class CustomHeaderHttpRequestInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String grayTag = request.getHeader("X-GRAY-TAG");
        log.info("=================获取请求头环境标记:{}",grayTag);
        GrayTagThreadLocal.set(grayTag);
        return true;

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        GrayTagThreadLocal.remove();
        log.info("-----请求完成删除请求标记:{}",request.getHeader("X-GRAY-TAG"));
    }
}
